/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.dao;

import com.mx.im.sinocab.bean.User;
import com.mx.im.sinocab.util.Conection;
import com.mx.im.sinocab.util.Constant;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Javier Aviles Cruz
 */
public class LoginDAO implements Constant{
    

    public User userVerification(User user){
        System.out.println("Se esta verificando usuario.....");
            PreparedStatement stat;
            Conection cone = new Conection();
        try {
            
        
            stat = cone.getConection().prepareStatement(SQL_USER_VERIFICATION);
            stat.setString(1, user.getUser());
            stat.setString(2, user.getPassword());


            ResultSet result = stat.executeQuery();

            //conexion.commit();
            //conexion.setAutoCommit(true);

            if (result.next()) {
                user = new User();
                user.setActiveSesion(true);
                //System.out.println("SE encontró a : "+ result.getInt("idUser"));
                user.getEmploy().setIdEmploy(Integer.parseInt(result.getObject(1).toString()));
                user.getEmploy().setName(result.getObject("name").toString());
                user.getEmploy().setLastName(result.getObject("lastName").toString());
                user.getEmploy().setAddress(result.getObject("address").toString());
                user.getEmploy().setPhone(result.getObject("phone").toString());
                user.getEmploy().setEmail(result.getObject("email").toString());
                //user.getEmploy().setBirthday(new Date(result.getObject("birthday").toString()));
                System.out.println("Se encontro a: " + Integer.valueOf(result.getObject(8).toString()));
                user.setIdUser(Integer.valueOf(result.getObject("idUser").toString()));
                user.setUser(result.getObject("user").toString());
                user.setPassword(result.getObject("password").toString());
                //user.setExpiry(new Date(result.getObject("expiry").toString()));
                
                user.getUsertype().setIdUserType(Integer.parseInt(result.getObject("idUserType").toString()));
                user.getUsertype().setDefinition(result.getObject("definition").toString());
                user.getUsertype().setDescription(result.getObject("description").toString());
                user.getEmploy().setGender(result.getObject("gender").toString());
            }

            if (stat != null) {
                stat.close();
            }
            
        } catch (NullPointerException npe) {
            System.out.println("UsiarioDAO - verificaEmpleado(): " + npe.getMessage());
            npe.printStackTrace();
        }catch (SQLException e) {
            System.out.println("UsiarioDAO - verificaEmpleado(): " + e.getMessage());
            e.printStackTrace();
        }catch(Exception e){
            System.out.println("UsiarioDAO - verificaEmpleado(): " + e.getMessage());
            e.printStackTrace();
        } finally {
            cone.closeConection();
            return user;
        }
    }
}
